using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace FileTracker.Forms
{
    public partial class FormEditFavorites : Form
    {
        ListViewItem _currentItem;
        //ApplicationSettings _configFile;
        bool _isEditMode = false;

        public FormEditFavorites()
        {
            InitializeComponent();
        }

        private void EditFavorites_Load(object sender, EventArgs argEventArgs)
        {
            ColumnHeader columnHeaderName;
            ColumnHeader columnHeaderPath;
            
            listViewFavorites.View = View.Details;
            listViewFavorites.FullRowSelect = true;
            listViewFavorites.CheckBoxes = false;

            columnHeaderName = new ColumnHeader();
            columnHeaderName.Text = "Name";
            columnHeaderName.Width = 90;
            columnHeaderName.TextAlign = HorizontalAlignment.Left;

            columnHeaderPath = new ColumnHeader();
            columnHeaderPath.Text = "Path";
            columnHeaderPath.Width = 225;
            columnHeaderPath.TextAlign = HorizontalAlignment.Left;

            listViewFavorites.Columns.Add(columnHeaderName);
            listViewFavorites.Columns.Add(columnHeaderPath);

            //ApplicationSettings
            //_configFile = new ApplicationSettings();
	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //Loop through Favorites
			foreach (KeyValuePair<string, string> CurrentFavorite in localSettings.FavoriteList)
            {
                ListViewItem itemNew;

                string favoriteName = CurrentFavorite.Key; 
                string favoritePath = CurrentFavorite.Value;

                itemNew = new ListViewItem(favoriteName);
                itemNew.SubItems.Add(favoritePath);

                listViewFavorites.Items.Add(itemNew);
            }

        }

        private void buttonEdit_Click(object sender, EventArgs argEventArgs)
        {
            if (listViewFavorites.SelectedItems.Count <= 0)
                return;

            _currentItem = listViewFavorites.SelectedItems[0];

            textBoxName.Text = _currentItem.SubItems[0].Text;
            textBoxPath.Text = _currentItem.SubItems[1].Text;

            _isEditMode = true;
        }

        private void buttonOK_Click(object sender, EventArgs argEventArgs)
        {
            if (textBoxPath.Text == "")
                return;

            //Check for path exists
            if (!Directory.Exists(textBoxPath.Text))
            {
                MessageBox.Show("That path does not exist.  Please try again.", "Invalid MenuItemFavoriteLeft path");
                textBoxPath.Focus();
                return;
            }

            //Check if user is editing an old entry
            if (_isEditMode)
            {
                _currentItem.SubItems[0].Text = textBoxName.Text;
                _currentItem.SubItems[1].Text = textBoxPath.Text;

                _isEditMode = false;
                _currentItem = null;
                textBoxName.Text = "";
                textBoxPath.Text = "";

                return;
            }

            //TO DO:Check for duplicates
            foreach(ListViewItem CurrentItem in listViewFavorites.Items)
            {
                //Check the name
                if (textBoxName.Text == CurrentItem.SubItems[0].Text)
                {
                    MessageBox.Show("There is already a MenuItemFavoriteLeft with that name.  Please try again.", "Invalid MenuItemFavoriteLeft name");
                    textBoxName.Focus();
                    return;
                }

                //Check the path
                if (textBoxPath.Text == CurrentItem.SubItems[1].Text)
                {
                    MessageBox.Show("There is already a MenuItemFavoriteLeft with that path.  Please try again.", "Invalid MenuItemFavoriteLeft path");
                    textBoxPath.Focus();
                    return;
                }
            }

            ListViewItem itemNew;

            itemNew = new ListViewItem(textBoxName.Text);
            itemNew.SubItems.Add(textBoxPath.Text);

            listViewFavorites.Items.Add(itemNew);

            _isEditMode = false;
            _currentItem = null;
            textBoxName.Text = "";
            textBoxPath.Text = "";
        }

        private void buttonSave_Click(object sender, EventArgs argEventArgs)
        {

            if (_isEditMode)
            {
                MessageBox.Show("You are still in edit mode.  Please finish editing or clear the selection.");
                return;
            }
	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

			localSettings.FavoriteList.Clear();

            //Loop through ListViewItems
            foreach (ListViewItem CurrentItem in listViewFavorites.Items)
            {
				localSettings.FavoriteList.Add(CurrentItem.SubItems[0].Text, CurrentItem.SubItems[1].Text);
            }

            //Save the config file.
			localSettings.Save();

            this.Close();

        }

        private void buttonDelete_Click(object sender, EventArgs argEventArgs)
        {
            if (listViewFavorites.SelectedItems.Count <= 0)
                return;

            listViewFavorites.SelectedItems[0].Remove();

            _currentItem = null;
            textBoxName.Text = "";
            textBoxPath.Text = "";
            _isEditMode = false;
        }

        private void buttonClear_Click(object sender, EventArgs argEventArgs)
        {
            _currentItem = null;

            textBoxName.Text = "";
            textBoxPath.Text = "";
            _isEditMode = false;
        }

        private void buttonPathBrowseDirectory_Click(object sender, EventArgs argEventArgs)
        {
            FolderBrowserDialog BrowseDefault;
            //TO DO:Open directory browser
            BrowseDefault = new FolderBrowserDialog();
            BrowseDefault.ShowNewFolderButton = false;
            BrowseDefault.RootFolder = Environment.SpecialFolder.DesktopDirectory;

            BrowseDefault.ShowDialog(this);
            textBoxPath.Text = BrowseDefault.SelectedPath;

        }

        private void buttonCancel_Click(object sender, EventArgs argEventArgs)
        {
            this.Close();
        }


    }
}